package com.milotnt.controller;

import com.milotnt.pojo.productEntity;
import com.milotnt.service.ProductService;
import com.milotnt.vo.ProductVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    //查询产品
    @RequestMapping("/selProduct")
    public String selectProduct(Model model, HttpSession session) {
        List<ProductVo> productList = productService.findAllProducts();
        session.setAttribute("productList", productList);
        model.addAttribute("productList", productList);
        log.info("查询所有产品列表：{}", productList);
        return "selectProduct";
    }

    //跳转新增产品页面
    @RequestMapping("/toAddProduct")
    public String toAddProduct(Model model) {
        List<ProductVo> productList = productService.findAllProducts();
        model.addAttribute("product", productList);
        return "addProduct";
    }

    //新增产品
    @RequestMapping("/addProduct")
    public String addProduct(productEntity product) {
        productService.insertProduct(product);
        return "redirect:selProduct";
    }

    //删除产品
    @RequestMapping("/delProduct")
    public String deleteProduct(Long productId) {
        productService.deleteByProductId(productId);
        return "redirect:selProduct";
    }

    //跳转修改产品页面
    @RequestMapping("/toUpdateProduct")
    public String toUpdateProduct(Long productId, Model model) {
        List<ProductVo> productList = productService.selectByProductId(productId);
        model.addAttribute("productList", productList);
        return "updateProduct";
    }

    //修改产品信息
    @RequestMapping("/updateProduct")
    public String updateProduct(productEntity product) {
        productService.updateProductByProductId(product);
        return "redirect:selProduct";
    }


}
